package com.idopartx.phonelightning.thread

import android.content.Context
import com.idopartx.phonelightning.entity.LightNightVo
import com.idopartx.phonelightning.utils.LightningUtils


class LightningThread(
    var context: Context,
    var lightNightList: MutableList<LightNightVo>?,
    var times: Int

) :
    Thread() {

    var run: Boolean = true

    fun stopThread() {
        run = false
    }

    override fun run() {
        if (times == 0) {
            while (run) {
                lightNightList?.forEach {

                    LightningUtils.get().toggle(context)
                    sleep(it.light.toLong())

                    LightningUtils.get().toggle(context)
                    sleep(it.night.toLong())

                }
            }
        } else {
            for (i in 0 until times) {
                if (!run) return

                lightNightList?.forEach {

                    LightningUtils.get().toggle(context)
                    sleep(it.light.toLong())

                    LightningUtils.get().toggle(context)
                    sleep(it.night.toLong())

                }
            }
        }
    }
}